package com.zeedev.flyingsaucer.global;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.zeedev.flyingsaucer.beers.modules.BeerModule;
import com.zeedev.flyingsaucer.modules.BeerDatabaseModule.modules.MemberModule;
import com.zeedev.flyingsaucer.modules.StoreModule.modules.StoreModule;
import com.zeedev.flyingsaucer.service.BeerKnurdServiceModule;
import mobi.zeedev.flyingsaucer.common.RegexModule.interfaces.IRegexQueries;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.modules.UfoClubClientModule;
import mobi.zeedev.flyingsaucer.common.WebQueryModule.modules.BeerWebQueryModule;
import mobi.zeedev.flyingsaucer.scala.regex.ScalaRegexQueries;
import roboguice.application.RoboApplication;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class UfoClubApp extends RoboApplication {

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Constructor
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    public UfoClubApp() {
        super();
        //Log.i(TAG, "Starting Flying Saucer UFO Club Application.");

    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Constants
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    private static final String TAG = "UfoClubApp";
    private static final String UFO_CLUB_APP_PREFS = "UfoClubAppGlobal.preferences";
    public static final String UFO_CLUB_APP_PREFS_FEB_2011 = "FlyingSaucerUfoClubFeb2011.preferences";
    private static final String LAST_USER = "LAST_USER";

    //    public static final Boolean DEBUG_WRITE_DB_SDCARD = false;
    public static final Boolean DEBUG_DISABLE_REFRESH_DATABASE = false;
    //    public static final Boolean DEBUG_LOAD_ALTERNATE_DB = false;
    public static final Boolean DEBUG_RESET_NEW_DATABASE = false;
//    public static final boolean DEBUG_UPGRADE_DATABASE_OFF = false;

    public static final String HAS_LOADED_EXTRAS = "HAS_LOADED_EXTRAS";
    private static final String LAST_BEER_LOOKUP = "LAST_BEER_LOOKUP";

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Guice
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    protected void addApplicationModules(List<Module> modules) {

        //First Set of global (currently empty)


//        modules.add(new CredentialsModule());
//
//        modules.add(new PreferenceCredentialsModule());


        modules.add(new UfoClubModule());

//        modules.add(new LoginModule());


        modules.add(new StoreModule());


        //GLOBAL

        //Independent (requires: Credentials)
        modules.add(new BeerWebQueryModule());

        //Independent
//        modules.add(new RegexModule());
        modules.add(new AbstractModule() {
            @Override
            protected void configure() {
                bind(IRegexQueries.class).to(ScalaRegexQueries.class).asEagerSingleton();
            }
        });


        //Requires: BeerWebQueryModule, RegexModule, CredentialsModule
        modules.add(new UfoClubClientModule());

        //Independent / Application level
        modules.add(new BeerKnurdServiceModule());


        //Database Modules
        modules.add(new MemberModule());


        //Android Activity Modules
        modules.add(new BeerModule());


    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Getters / Setters for global data
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    public Date getLastBeerLookup() {

        Long l = (Long) UfoClubApp.getGeneralPreferences(this, UFO_CLUB_APP_PREFS, LAST_BEER_LOOKUP);
        if (l == null) {
            return null;
        } else if (l == 0) {
            return null;
        } else if (l > 0) {
            Date d = new Date();
            d.setTime(l);
            return d;
        } else {
            return null;
        }
//		return lastBeerLookup;
    }

    public void setLastBeerLookup(Date lastBeerLookup) {
//		this.lastBeerLookup = lastBeerLookup;
        if (lastBeerLookup != null) {
            UfoClubApp.setGeneralPreference(this, UFO_CLUB_APP_PREFS, LAST_BEER_LOOKUP, lastBeerLookup.getTime());
        } else {
            UfoClubApp.setGeneralPreference(this, UFO_CLUB_APP_PREFS, LAST_BEER_LOOKUP, 0l);
        }

    }

    public Boolean hasLoadedExtraBeerInfo() {
        Object o = UfoClubApp.getGeneralPreferences(this, UFO_CLUB_APP_PREFS, HAS_LOADED_EXTRAS);
        Long l = null;
        Boolean ret = false;
        if (o != null) {
            l = Long.parseLong(o.toString());
        }

        if (l != null && l == 0l) {
            ret = false;
        } else if (l != null && l > 0) {
            ret = true;
        } else {
            ret = false;
        }
        return ret;
    }

    public void setLoadedExtraBeerInfo(Boolean hasLoaded) {
        if (hasLoaded) {
            UfoClubApp.setGeneralPreference(this, UFO_CLUB_APP_PREFS, HAS_LOADED_EXTRAS, Calendar.getInstance().getTime().getTime());
        } else {
            UfoClubApp.setGeneralPreference(this, UFO_CLUB_APP_PREFS, HAS_LOADED_EXTRAS, 0l);
        }
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Data
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    private Date lastBeerLookup;

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Business Logic
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    public Integer getLastMember() {
        SharedPreferences prefs = this.getSharedPreferences(UFO_CLUB_APP_PREFS, Context.MODE_PRIVATE);
        return prefs.getInt(LAST_USER, 0);
    }

    public void setLastMember(Integer memberId) {
        if (memberId != null) {
            SharedPreferences prefs = this.getSharedPreferences(
                    UFO_CLUB_APP_PREFS, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt(LAST_USER, memberId);
            editor.commit();
        }
    }

    @SuppressWarnings("unchecked")
    public static void setGeneralPreference(Context context, String prefs, String property, Object value) {
        if (property != null) {
            SharedPreferences specifiedPrefs = context.getSharedPreferences(
                    prefs, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = specifiedPrefs.edit();
            //editor.put .putInt(property, value);
            Class valueClass = value.getClass();
            if (valueClass == String.class) {
                editor.putString(property, (String) value);
            } else if (valueClass == Integer.class) {
                editor.putInt(property, (Integer) value);
            } else if (valueClass == Long.class) {
                editor.putLong(property, (Long) value);
            } else if (valueClass == Boolean.class) {
                editor.putBoolean(property, (Boolean) value);
            } else if (valueClass == Float.class) {
                editor.putFloat(property, (Float) value);
            }
            editor.commit();
        }
    }

    public static Object getGeneralPreferences(Context context, String prefs, String property) {
        Object ret = null;
        SharedPreferences specifiedPrefs = context.getSharedPreferences(prefs, Context.MODE_PRIVATE);
        Map<String, ?> allprefs = specifiedPrefs.getAll();
        for (Entry<String, ?> entry : allprefs.entrySet()) {
            if (entry.getKey().equals(property)) {
//				Class valueClass = entry.getValue().getClass();
                return entry.getValue();
                /*		if (valueClass == String.class) {
                        //editor.putString(property, (String) value );
                        return o;
                    } else if (valueClass == Integer.class) {
                        editor.putInt(property, (Integer) value);
                    } else if (valueClass == Long.class) {
                        editor.putLong(property, (Long) value);
                    } else if (valueClass == Boolean.class) {
                        editor.putBoolean(property, (Boolean) value);
                    } else if (valueClass == Float.class) {
                        editor.putFloat(property, (Float) value);
                    }		*/
            }
        }
        //Class valueClass = editor

        //return specifiedPrefs.getInt(LAST_USER, 0);
        return ret;
    }


    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Life-cycle
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


    //This will bind the singleton credentials object to the preferences on startup
//    @Inject
//    @Named("autoIPreferenceCredentials")
//    protected IPreferenceCredentials preferenceCredentials;

    @Override
    public void onCreate() {

        super.onCreate();

//		Debug.startMethodTracing("UfoClubApp");

        lastBeerLookup = null;


/*        if (DEBUG_LOAD_ALTERNATE_DB) {
    Date t = new Date();
    //t.setTime(1279508632785l);
    t.setTime(1278113758447l);
    credentials.setOriginDate(t);
    //credentials.setMemberID(186919);
    //1278113758447

}*/

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

}
